#include <head.h>

// 定义共享内存结构体
struct shared_memory 
{
    long type; // 消息类型
    char data[100]; // 消息内容
};

int main(int argc, char const *argv[]) 
{
    key_t key;
    int shmid;
    struct shared_memory *shared_mem;

    // 生成键值
    key = ftok("progfile", 65);

    // 创建共享内存
    shmid = shmget(key, sizeof(struct shared_memory), 0666 | IPC_CREAT);
    if (shmid == -1) 
    {
        perror("shmget failed");
        exit(1);
    }

    // 将共享内存附加到进程的地址空间
    shared_mem = (struct shared_memory *)shmat(shmid, NULL, 0);
    if (shared_mem == (void *)-1) 
    {
        perror("shmat failed");
        exit(1);
    }

    // 设置消息类型和内容
    shared_mem->type = 1;
    strcpy(shared_mem->data, "Hello, World!");

    printf("共享内存已更新: %s\n", shared_mem->data);

    // 分离共享内存
    if (shmdt(shared_mem) == -1) 
    {
        perror("shmdt failed");
        exit(1);
    }

    return 0;
}